import pymysql
import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

# 数据库配置
DB_CONFIG = {
    'host': os.getenv('DB_HOST', 'localhost'),
    'port': int(os.getenv('DB_PORT', 3306)),
    'user': os.getenv('DB_USER', 'root'),
    'password': os.getenv('DB_PASSWORD', ''),
    'database': os.getenv('DB_NAME', 'freefishchn'),
    'charset': 'utf8mb4'
}

def check_table_structure():
    """检查video_dispose表的结构"""
    try:
        # 连接数据库
        connection = pymysql.connect(**DB_CONFIG)
        with connection.cursor() as cursor:
            # 获取表结构
            cursor.execute("DESCRIBE video_dispose")
            columns = cursor.fetchall()
            
            print("video_dispose表结构:")
            print("-" * 50)
            for column in columns:
                print(f"字段名: {column[0]}, 类型: {column[1]}, 是否为空: {column[2]}, 主键: {column[3]}, 默认值: {column[4]}, 额外信息: {column[5]}")
            
            # 检查是否有数据
            cursor.execute("SELECT COUNT(*) as count FROM video_dispose")
            count = cursor.fetchone()
            print(f"\n表中记录数: {count['count']}")
            
            # 查看最近几条记录的response字段
            cursor.execute("SELECT task_id, status, response FROM video_dispose ORDER BY created_at DESC LIMIT 5")
            records = cursor.fetchall()
            print("\n最近5条记录的response字段:")
            print("-" * 50)
            for record in records:
                print(f"任务ID: {record['task_id']}, 状态: {record['status']}, 响应: {record['response']}")
                
    except Exception as e:
        print(f"检查表结构时出错: {e}")
    finally:
        if 'connection' in locals():
            connection.close()

if __name__ == "__main__":
    check_table_structure()